import { MarkerType } from 'reactflow';
import { merge } from 'lodash-es';

/**
 * 设置节点
 * @param setNodes {function}
 * @param values  {object}
 */
export function updateNodes({ setNodes, node, values }) {
  const {
    label,
  } = values;

  const _node = {
    ...node,
    data: {
      label,
      width: node.width,
      height: node.height,
    }
  }
  // console.log('_node', _node)

  setNodes((nds: any[]) => {
    // it.selected
    return nds.map(it => (it.id === node.id) ? _node : it);
  });
}

/**
 * 设置连线
 * @param setEdges {function}
 * @param values {object}
 */
export function updateEdges({ setEdges, edge, values }) {
  // const {
  //   label,
  //   type,
  //   markerEnd,
  // } = values;

  const newConnection = {
    ...edge,
    ...values
  }
  console.log('newConnection', newConnection)
  setEdges((eds: any[]) => {
    console.log('setEdges', eds)
    return eds.map(it => (it.id === edge.id) ? newConnection : it);
  });

  // setEdges((els: any[]) => {
  //   const oldEdge = els.find(it => it.id === edge.id);
  //   return updateEdge(oldEdge, newConnection, els);
  // });
}
